import { uploadImage } from "../../../../utils/utils";
import area from "../../../../utils/area";
import { useRequest } from "../../../../utils/request";

const WeValidator = require('../../../../package/we-validator.min')

Component({
    properties: {},
    observers: {
        'houseImage': function (data) {
            this.setData({
                imagesKeys: Object
                    .keys(data)
                    .sort((a, b) => data[a].index - data[b].index),
            })
        },
    },
    data: {
        areaList: area,
        checkboxValue: [],
        // 标题图
        image: '',
        // 房源图
        houseImage: {
            room: {
                imgs: [],
                index: 1
            },
            hall: {
                imgs: [],
                index: 2
            },
            wash: {
                imgs: [],
                index: 3
            },
            kitchen: {
                imgs: [],
                index: 4
            },
            around_eat: {
                imgs: [],
                index: 5
            },
            around_fun: {
                imgs: [],
                index: 6
            },
            other: {
                imgs: [],
                index: 7
            },
        },
        // 是否显示城市选择
        showCityChange: false,
        // 城市选择
        province: '',
        city: '',
        area: '',
        // 房源类型
        houseType: null,
        // 房源户型
        houseRoomNum: {
            // 房间数量
            room_count: 0,
            // 大厅数量
            hall_count: 0,
            // 浴室数量
            wash_count: 0,
            // 厨房数量
            kitchen_count: 0,
            // 阳台
            balcony_count: 0,
        }
    },
    methods: {
        onLoad(options) {
            if (options.id) {
                this.houseDataRequest = new useRequest('api/housedetail', {
                    params: { house_id: options.id },
                    data: 'editHouseData',
                    hold: true
                }, this)
                this.houseDataRequest.run().then(data => {
                    if (this.data.houseData) {
                        const index = Object.keys(this.data.houseData.house_types).findIndex(item => item == data.house_typeid)
                        const key = Object.keys(this.data.houseData.house_types)[index]
                        const d = this.data.houseData.house_types[key]
                        this.setData({
                            houseType: Object.assign(d, { id: key })
                        })
                    }
                    this.setData({
                        ...data,
                        image: data.img,
                        houseImage: data.house_imgs,
                        houseRoomNum: {
                            room_count: data.room_count,
                            hall_count: data.hall_count,
                            wash_count: data.wash_count,
                            kitchen_count: data.kitchen_count,
                            balcony_count: data.balcony_count,
                        },
                        houseEquipment: data.part_set,
                        houseEquipmentText: data.part_set.wash
                            .concat(data.part_set.equip)
                            .concat(data.part_set.base)
                            .concat(data.part_set.other)
                            .join('、'),
                        checkboxValue: data.live_limit
                    })
                })
            }
            this.request = new useRequest('api/housebaseset', {
                data: 'houseData',
                pretreatment: data => {
                    if (options.id && this.data.title) {
                        const index = Object.keys(data.house_types).findIndex(item => item == this.data.house_typeid)
                        const key = Object.keys(data.house_types)[index]
                        const d = data.house_types[key]
                        this.setData({
                            houseType: Object.assign(d, { id: key }),
                        })
                    }
                    this.setData({
                        description: data.room_types[0].description
                    })
                    return data
                }
            }, this)
            this.initValidator()
        },
        initValidator() {
            this.validatorInstance = new WeValidator({
                rules: {
                    title: {
                        required: true
                    },
                    room_type: {
                        required: true,
                    },
                    house_area: {
                        required: true
                    },
                    price: {
                        required: true
                    },
                    address: {
                        required: true
                    },
                },
                messages: {
                    title: {
                        required: '请输入标题'
                    },
                    room_type: {
                        required: '请选择房屋类型',
                    },
                    house_area: {
                        required: '请输入出租面积'
                    },
                    price: {
                        required: '请输入出租价格'
                    },
                    address: {
                        required: '请输入详细地址'
                    },
                },
            })
        },
        /**
         * 提交
         * @param e
         */
        submit(e) {
            const value = e.detail.value

            if (!this.data.image) {
                wx.showToast({
                    icon: 'none',
                    title: '请上传标题图'
                })
                return
            }

            if (!this.data.houseImage.room.imgs.length) {
                wx.showToast({
                    icon: 'none',
                    title: '请上传房源图片'
                })
                return
            }

            if (!this.data.houseType) {
                wx.showToast({
                    icon: 'none',
                    title: '请选择房源类型'
                })
                return
            }

            if (!this.data.houseEquipmentText) {
                wx.showToast({
                    icon: 'none',
                    title: '请选择配套设施'
                })
                return
            }

            if (!this.data.houseRoomNum.room_count) {
                wx.showToast({
                    icon: 'none',
                    title: '请选择房源户型'
                })
                return
            }

            if (!this.data.province) {
                wx.showToast({
                    icon: 'none',
                    title: '请选择所在地区'
                })
                return
            }

            if (this.validatorInstance.checkData(value)) {
                wx.navigateTo({
                    url: '/pages/house/edit/release/release',
                    success: (res) => {
                        res.eventChannel.emit('getFormValues', {
                            id: this.data.house_id || 0,
                            editHouseData: this.data.editHouseData || null,
                            // 输入和选择的内容
                            ...value,
                            // 图片
                            img: this.data.image,
                            house_imgs: this.data.houseImage,
                            // 房源类型
                            house_type: this.data.houseType.id,
                            // 房源户型
                            ...this.data.houseRoomNum,
                            // 配套设施
                            house_set: this.data.houseEquipment,
                            // 地区
                            province: this.data.province,
                            city: this.data.city,
                            area: this.data.area,
                            // 入住要求,
                            live_limit: this.data.checkboxValue
                        })
                    }
                })
            }
        },
        /**
         * 选择入住要求
         * @param e
         */
        onCheckboxChange(e) {
            this.setData({
                checkboxValue: e.detail
            })
        },
        /**
         * 上传主图
         */
        chooseMainImage() {
            uploadImage().then(url => this.setData({
                image: url[0]
            }))
        },
        /**
         * 去上传房源照片
         */
        toUploadImage() {
            wx.navigateTo({
                url: '/pages/house/edit/images/images',
                events: {
                    uploadHouseImages: (data) => {
                        this.setData({
                            houseImage: data
                        })
                    }
                },
                success: (res) => {
                    res.eventChannel.emit('getHouseImage', this.data.houseImage)
                }
            })
        },
        /**
         * 显示城市
         */
        showCity() {
            this.setData({
                showCityChange: true
            })
        },
        /**
         * 隐藏城市
         */
        hideCity() {
            this.setData({
                showCityChange: false
            })
        },
        /**
         * 选择城市
         * @param e
         */
        changeCity(e) {
            this.hideCity()
            const data = e.detail.values
            this.setData({
                province: data[0].name,
                city: data[1].name,
                area: data[2].name,
            })
        },
        /**
         * 去选择房源类型
         */
        chooseHouseType() {
            wx.navigateTo({
                url: '/pages/house/edit/type/type',
                events: {
                    setHouseType: data => {
                        this.setData({
                            houseType: data
                        })
                    }
                },
                success: res => {
                    res.eventChannel.emit('getHouseType', this.data.houseData.house_types)
                }
            })
        },
        /**
         * 去选择房源户型
         */
        chooseRoomNum() {
            wx.navigateTo({
                url: '/pages/house/edit/room-nums/room-nums',
                events: {
                    setHouseRoomNum: data => {
                        this.setData({
                            houseRoomNum: data
                        })
                    }
                },
                success: res => {
                    res.eventChannel.emit('getHouseRoomNum', this.data.houseRoomNum)
                }
            })
        },
        /**
         * 去选择配套设施
         */
        chooseHouseEquipment() {
            wx.navigateTo({
                url: '/pages/house/edit/equipment/equipment',
                events: {
                    setHouseEquipment: data => {
                        const houseEquipmentText = data.wash
                            .concat(data.equip)
                            .concat(data.base)
                            .concat(data.other)
                            .join('、')
                        this.setData({
                            houseEquipment: data,
                            houseEquipmentText
                        })
                    }
                },
                success: res => {
                    res.eventChannel.emit('getHouseEquipment', this.data.houseEquipment, this.data.houseData.part_set)
                }
            })
        },
        changeHouseType(e) {
            const value = e.detail.value
            this.setData({
                description: this.data.houseData.room_types.find(item => item.value == value).description
            })
        }
    }
});
